//
//  STComposePictureView.swift
//  STVVeibo
//
//  Created by leergou on 16/7/27.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

let ComposePictureViewCellID = "ComposePictureViewCellID"
class STComposePictureView: UICollectionView {
    
    // 定义一个闭包,当点击 + 号按钮时,通知 composeController,弹出相册
    var closure:(()->())?
    // 保存图片的数组
    var imageList: [UIImage] = [UIImage]()
    // MARK: 重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 添加图片到数组
    func addImage(image: UIImage){
        // 添加图片到数组
        imageList.append(image)
        // 显示配图
        hidden = false
        // 刷新数据
        reloadData()
    }
    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 第一次进来,默认隐藏
        hidden = true
        // 设置 layout
        let cellMargin: CGFloat = 5
        let cellWidth = (SCREENW - 20 - cellMargin * 2) / 3
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellWidth, height: cellWidth)
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
        
        // 设置代理和数据源
        dataSource = self
        delegate  = self
        // 注册 cell
        registerClass(STComposePictureViewCell.self, forCellWithReuseIdentifier: ComposePictureViewCellID)
    }
}

//MARK: collectionView delegate & dataSource
extension STComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = imageList.count
        // 0 或者 9时,不显示 + 号图片
        if count == 0 || count == 9 {
            return count
        }
        // 其他的显示,多一个 cell,显示 + 号图片
        return imageList.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ComposePictureViewCellID, forIndexPath: indexPath) as! STComposePictureViewCell
        
//        cell.backgroundColor = RandomColor()
        if indexPath.item == imageList.count {
            cell.image = nil
        } else {
            cell.image = imageList[indexPath.item]
        }
        
        // 点击 cell 上 + 号按钮的回调
        cell.closure = {[weak self] in
            // 移除指定的 数组中 的image
            self?.imageList.removeAtIndex(indexPath.item)
            if self?.imageList.count == 0 {
                self?.hidden = true
            }
            // 刷新数据
            self?.reloadData()
        }
        
        return cell
    }
    
    // cell 点击
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // 点击的是 + 号按钮
        if indexPath.item == imageList.count {
            closure?()
        }
    }
}

//MARK: 自定义 cell
class STComposePictureViewCell:UICollectionViewCell{
    // 定义一个闭包
    var closure:(()->())?
    // 定义一个属性,供外界赋值
    var image: UIImage?{
        didSet{
            
            if image == nil {
                // 说明是 + 号 的 cell
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                // 隐藏删除按钮
                deleteButton.hidden = true
            } else {
                // 正常图片的按钮
                bgImageView.image = image
                // 显示删除按钮
                deleteButton.hidden = false
            }
        }
    }
    
    //MARK: 重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 删除按钮点击
    func deleteButtonClick(){
        // 也调用闭包--> 点击 + 号 按钮的时候也调用这个闭包
        closure?()
    }
    
    //MARK: 设置视图
    private func setupUI(){
        // 添加子控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
    }
    
    //MARK: lazy
    // 图片
    private lazy var bgImageView: UIImageView = UIImageView()
    // 右上角的删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton(title: nil, imageName: "compose_photo_close", target: self, action: "deleteButtonClick")
        return button
    }()
}























